package com.cango.student.algorithm.leetcode;

import com.cango.student.algorithm.nodepackage.Node;

/**
 * reverse-nodes-in-k-group
 */
public class ReverseKGroup {
    public Node reverseKGroup(Node head, int k) {
        Node current_node = head;
        int count = 0;
        while (current_node != null && count != k) {
            current_node = current_node.next;
            count++;
        }
        if (count == k) {
            current_node = reverseKGroup(current_node, k);/// 递归的解决子问题
            while (count-- > 0) {
                Node temp = head.next;
                head.next = current_node;
                current_node = head;
                head = temp;
            }///最终，该段的所有节点将会截空，head应指向current_node
            head = current_node;
        }
        return head;
    }
}
